import VueRouter from 'vue-router'
import Vue from 'vue'

import login from '../views/login/login.vue'
import layout from '../views/layout/layout.vue'
import chart from '../views/layout/chart/chart.vue'
import user from '../views/layout/user/user.vue'
import outline from '../views/layout/outline/outline.vue'
import building from '../views/layout/building/building.vue'
import subject from '../views/layout/subject/subject.vue'

Vue.use(VueRouter)
const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: login,
            meta: {
                title: "黑马面面",
                roles: ["超级管理员", "管理员", "老师", "学生"]

            }
        },
        {
            path: '/layout',
            component: layout,
            redirect: "/layout/subject",
            children: [
                {
                    path: "/layout/chart",
                    component: chart,
                    meta: {
                        title: "数据概览",
                        icon: "el-icon-pie-chart",
                        roles: ["超级管理员", "管理员", "老师",]
                    }
                },
                {
                    path: "/layout/user",
                    component: user,
                    meta: {
                        title: "用户列表",
                        icon: "el-icon-user",
                        roles: ["超级管理员", "管理员",]

                    }
                },
                {
                    path: "/layout/outline",
                    component: outline,
                    meta: {
                        title: "题库列表",
                        icon: "el-icon-edit-outline",
                        roles: ["超级管理员", "管理员", "老师",]

                    }
                },
                {
                    path: "/layout/building",
                    component: building,
                    meta: {
                        title: "企业列表",
                        icon: "el-icon-office-building",
                        roles: ["超级管理员", "管理员", "老师",]

                    }
                },
                {
                    path: "/layout/subject",
                    component: subject,
                    meta: {
                        title: "学科列表",
                        icon: "el-icon-notebook-2",
                        roles: ["超级管理员", "管理员", "老师", "学生"]

                    }
                },
            ]
        }
    ]
})

// 导入
import Nprogress from "nprogress";
import 'nprogress/nprogress.css'
import store from '@/store/index.js'
import { Message } from 'element-ui';
// import { removeLocal } from "@/utils/local"

// 离开时拦截 路由前置守卫
router.beforeEach((to, from, next) => {
    // 开启进度
    Nprogress.start()
    // window.console.log("to", to);
    window.console.log("from", from);
    if (to.meta.roles.includes(store.state.role)) {
        next()
    } else {
        next("/subject")
        Message.error("你无权访问该页面")
    }
})

// 进入后拦截 路由后置守卫
router.afterEach((to, from) => {
    // 结束进度进度
    Nprogress.done()

    // 修改标题简写模式
    document.title = to.meta.title
    // window.console.log("to", to);
    window.console.log("from", from);
})


export default router